﻿/*
 MARQUEDROPPOS.SQF
 =================
 © Pumpkin

 Scritp permettant de determiner un position de largage de ravitaillement, soit par click-sur-carte, soit par fumigène

 paramètres:
 nul = [ammo, [materiel1,materiel2,...], attente (,type d'avion)] execVM "AirSupply\MarqueDropPos.sqf"

 1. type de munition de repèrage ou mode carte : si =à chaine vide "", le mode click-sur-carte est activé, sinon
    le nom exact de la munition qui sert à l'indication de la position de parachutage (voir rappel ci-dessous)
 2. temps d'attente avant la venu de l'avion ravitailleur sur site de parachutage
 3. tableau du materiel  larger : 2 manières de le remplir : soit avec le nom des objets -CRÉER AU SOL-, soit avec une chaine vide comme 1er élément
                                   suivi de la classe d'objet à générer; la 1ère façon permet de modifier l'objet (ajout de munitions,...) et la
                                   gestion de l'objet grace à sa variable nom; la 2ème façon permet la génération de matériel à volonté.
 4. type d'avion : (optionnel) "MV22" par défaut (pour ARMA2; avion plus maniable que le C130J)

 RAPPEL : _ammo =
 SmokeShell
	SmokeShellRed
	SmokeShellGreen
	SmokeShellYellow
	SmokeShellPurple
	SmokeShellBlue
	SmokeShellOrange
	G_40mm_Smoke
	G_40mm_SmokeRed
	G_40mm_SmokeGreen
	G_40mm_SmokeYellow
*/


Server_Detected = call compile preprocessFile "scripts\common.sqf";
if (!X_Client) exitwith {};


if (player != leader group player) exitwith {hint "Only leaders can call in AirDrops"};
_ammo = _this select 0;
_supplies = _this select 1;
_attente = _this select 2;
_planetype = "MV22";
_tempo = -1;
_mapOpen = True;
diag_log Format["Initializing Air Drop with %1, %2, %3, %4", _ammo, _supplies, _attente, _planeType];

if (count _this > 3) then {
	_param = _this select 3;
	if (typename _param == "STRING") then { _planetype = _param };
	if (typename _param == "SCALAR") then { _tempo = _param };
};

if (count _this > 4) then {
	_param = _this select 4;
	if (typename _param == "STRING") then { _planetype = _param };
	if (typename _param == "SCALAR") then { _tempo = _param };
};

if (PUMPKINMarkLarguePos) exitwith {pilote sidechat "Air Supply already in progress... Please try again later"};
PUMPKINMarkLarguePos = true;
publicvariable "PUMPKINMarkLarguePos";

if (_ammo == "") then { 
	diag_log Format["Using Map Pointer"];
	_markerObj = createMarkerLocal ["AirSupply_Drop_Zone", getPos player];
	"AirSupply_Drop_Zone" setMarkerShapeLocal "ELLIPSE";
	"AirSupply_Drop_Zone" setMarkerColorLocal "ColorGreen";
	"AirSupply_Drop_Zone" setMarkerSizeLocal [100,100];
	_markerObj setMarkerAlpha 0.5;
	_mapOpen = True;
	nul = [] execVM "scripts\AirSupply\ASDialogMenu.sqf";
} 
else { hint "Launch a Smoke Grenade to mark the position"; };

// Timeout en secondes
_initialtime = daytime;
_timelimit = 60;
_gotDropPos = false;
d_global_airdrop_pos = [];
diag_log Format["Start wait for Air Drop Position"];
while {not _gotDropPos} do {
	_timeout = (daytime - _initialtime) * 3600;
	if (_timeout > _timelimit) exitWith {
			diag_log Format["Air Drop Timed Out"];
			_gotDropPos = false;
			if (_mapOpen) then { closedialog 0; };
		};

 	// Position par grenade fumigène
 	if (_ammo != "") then {
		_grdshell = (player nearObjects [_ammo, 300]) select 0;
		_ammopos = getpos _grdshell;
		_hitposgnd = _ammopos;
		if (format["%1",_ammopos] != "ARRAY") then {
			while {((velocity _grdshell select 0) != 0) and ((velocity _grdshell select 1) != 0) and ((velocity _grdshell select 2) != 0)} do {
				_hitposgnd = _ammopos;
				_ammopos = GetPos _grdshell;
				sleep 0.01;
				player globalchat format ["_hitposgnd %1",_hitposgnd];
			};
			d_global_airdrop_pos = _hitposgnd;
		};
	};

	If (count d_global_airdrop_pos > 0) then {
		_gotDropPos = true;
	};
	sleep 0.1;
};
diag_log Format["Given drop position: %1 (%2)", d_global_airdrop_pos, _gotDropPos];

_markerPos = getMarkerPos "AirSupply_Drop_Zone";
If (((_markerPos select 0) != 0) OR ((_markerPos select 1) != 0) OR ((_markerPos select 2) != 0)) then {
	deleteMarkerLocal "AirSupply_Drop_Zone";
};

if (_gotDropPos) then {
	_playerPos = getPos player;
	_dropDistX = abs((_playerPos select 0) - (d_global_airdrop_pos select 0));
	_dropDistY = abs((_playerPos select 1) - (d_global_airdrop_pos select 1));
	if ((_dropDistX > 100) Or (_dropDistY > 100)) then {
		hint "The position is too far away, please keep it closer to you!";
	}
	else {
		diag_log Format["Calling the AirDrop: %1, %2, %3, %4, %5", d_global_airdrop_pos,_supplies,_attente,_planetype,_tempo];
		[d_global_airdrop_pos,_supplies,_attente,_planetype,_tempo] execVM "scripts\AirSupply\ASDropping.sqf";
	};
};

PUMPKINMarkLarguePos = false;
publicvariable "PUMPKINMarkLarguePos";
